From Koa introduction:
Koa is a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs. Through leveraging generators Koa allows you to ditch callbacks and greatly increase error-handling. Koa does not bundle any middleware within core, and provides an elegant suite of methods that make writing servers fast and enjoyable.
Requirements
Koa requires Node 7.6.0
or higher for async/await
syntax.
You can use older Node versions with Babel (see more about it at Koa installation section).
PostgreSQL version does not matter. We will use the latest one.
Environment
Node.js
If Node is not installed yet, you can download it here. If you are a lucky owner of Linux or macOS, package managers can be used - this method described here.
To check that the Node installation is correct, you can run the command:
1 | $ node --version |
That will print the Node version.
Also for NPM:
1 | $ npm --version |
Dependencies installation
Create directory where all the code will be located. Run terminal and open this directory.
Let’s initialize the package environment:
1 | $ npm init |
NPM will ask you a few questions. Type answers for package name of author.
1 | $ npm init |
You should see the package.json
file that NPM has created.
Install koa
:
1 | $ npm i koa |
Also we need something for requests routing. We will use a koa-router
- popular and full-featured package.
Another one - koa-logger
- for development logging.
1 | $ npm i koa-router koa-logger |
For database we will use a node-postgres
driver. It’s a simple client, not a ORM.
1 | $ npm i pg |
PostgreSQL
You can download PostgreSQL here with a portable binaries for fast start w/o installation proccess.
To check the PostgreSQL installation, run the command:
1 | $ sudo -u postgres psql |
On Windows (type your own Windows user name instead of postgres
, if you didn’t create a database manually):
1 | > psql -U postgres |
On Linux and macOS a PostgreSQL server should be running yet after installation via package manager.
Otherwise, or if you have Windows, follow next commands:
1 | > initdb -U postgres -D data |
A default postgres
database username is recommended, data
- a database storage directory, you can provide any other path.
After that a pg_ctl
command will start the PostgreSQL server (provide a path to the database storage).
Check again, you should see a PostgreSQL prompt:
1 | postgres=# |
Type \q
to exit.
Default database postgres
was created for us.
Hello, World!
Create file app.js
and copy the next code. It’s a modified version of standart Koa example.
1 | const Koa = require('koa'); |
Now you can start the server:
1 | $ node app.js |
If the server has successfully started, open the browser and go to http://localhost:3000/
. You should see the Hello, World!
page. Also, try to type your name, for example, http://localhost:3000/Nariman
.
In your terminal you should see something like that:
1 | $ node app.js |
Let’s connect to the database.
We will use the connection pool, because it’s a common way to use database drivers, especially for PostgreSQL. Pool has advatages over manual connections, due to each new manual connection can take some time for connection establishment and it requires some management to control the number of simultaneous connections.
Add the dependency:
1 | const { Pool } = require('pg'); |
Right before the server booting code (before app.listen
call):
1 | app.pool = new Pool({ |
Then, change our routes, to query database for Hello
string.
1 | router.get('/', async (ctx) => { |
Note that Koa can auto-translate the result into JSON, if the response is not a string. If you just return rows
, you’ll see the JSON response.
Save and restart the server.
In conclusion
We wrote everything in one file. Of course, for a real project, you should create the project structure, and also take care of closing the PostgreSQL pool and Koa connections when shutting down the server.
You can also use other database libraries, like Sequelize, Bookshelf, Massive.
References
- Node deps
- PostgreSQL
- How To Install and Use PostgreSQL on Ubuntu 16.04
- Building a RESTful API with Koa and Postgres
- Building A Server-Side Application With Async Functions and Koa 2
- Koa examples
- Koa wiki - links to the other useful stuff for Koa.
- Awesome Node.js